package com.e3.basdoc.cust.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.e3.basdoc.cust.dao.CustDAO;
import com.e3.basdoc.cust.entity.CustAccountEntity;
import com.e3.basdoc.cust.entity.CustEntity;
import com.e3.basdoc.cust.model.CustAccountModel;
import com.e3.basdoc.cust.model.CustModel;
import com.e3.basdoc.cust.model.CustQC;
import com.e3.basdoc.cust.service.CustQueryService;
import com.e3.core.base.DAOHelper;
import com.e3.core.base.ServiceException;
import com.e3.core.base.SuperQueryService;

public class CustQueryServiceImpl extends SuperQueryService implements CustQueryService {

	private CustDAO custDAO;

	private CustDAO getCustDAO() {
		if (custDAO == null) {
			custDAO = DAOHelper.createDAOInstance(CustDAO.class);
		}
		return custDAO;
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.cust.service.impl.CustQueryService#getCustModelById(long)
	 */
	public CustModel getCustModelById(long id) throws ServiceException {
		CustEntity cust = getCustDAO().getById(CustEntity.class, id);
		return custEntityToModel(cust);
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.cust.service.impl.CustQueryService#getCustModelByIds(java.lang.Long)
	 */
	public List<CustModel> getCustModelByIds(Long... ids) throws ServiceException {
		List<CustEntity> custEntities = getCustDAO().findBySingleField(CustEntity.class, "id", ids);

		if (custEntities != null && custEntities.size() > 0) {
			List<CustModel> custModels = new ArrayList<CustModel>(custEntities.size());
			for (CustEntity custEntity : custEntities) {
				custModels.add(custEntityToModel(custEntity));
			}
			return custModels;
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.cust.service.impl.CustQueryService#getCustByCondition(com.e3.basdoc.cust.model.CustQC)
	 */
	public List<CustModel> getCustByCondition(CustQC custQC) throws ServiceException {
		List<CustModel> custModels = new ArrayList<CustModel>();
		List<CustEntity> custs = getCustDAO().getCustsByCondition(custQC);
		if (custs != null) {
			for (CustEntity cust : custs) {
				custModels.add(custEntityToModel(cust));
			}
		}
		return custModels;
	}

	/*
	 * (non-Javadoc)
	 * @see com.e3.basdoc.cust.service.impl.CustQueryService#getCustChildren(com.e3.basdoc.cust.model.CustModel)
	 */
	public List<CustModel> getCustChildren(CustModel custModel) throws ServiceException {
		if(custModel == null){
			return null;
		}
		long pId = custModel.getId();
		List<CustEntity> entities = getCustDAO().findBySingleField(CustEntity.class, "parentId", new Long[] { pId });

		if (entities != null) {
			List<CustModel> custModels = new ArrayList<CustModel>(entities.size());
			for (CustEntity entity : entities) {
				CustModel custMd = custEntityToModel(entity);
				custModels.add(custMd);
			}
			return custModels;
		}

		return new ArrayList<CustModel>(0);
	}

	private CustModel custEntityToModel(CustEntity cust) {
		CustModel custModel = new CustModel();
		custModel = super.ocEntityToModel(custModel, cust);
		custModel.setId(cust.getId());

		// 基本信息
		custModel.setCode(cust.getCode());
		custModel.setName(cust.getName());
		custModel.setShortName(cust.getShortName());
		custModel.setType(cust.getType() + "");
		custModel.setParentId(cust.getParentId() );
		custModel.setRegionId(cust.getRegionId());
		custModel.setBusinessAddress(cust.getBusinessAddress());
		custModel.setContactAddress(cust.getContactAddress());
		custModel.setFax(cust.getFax());
		custModel.setZipcode(cust.getZipcode());
		custModel.setTelphone(cust.getTelphone());
		custModel.setEmail(cust.getEmail());
		custModel.setRemark(cust.getRemark());

		// 注册信息
		custModel.setRegisterAddress(cust.getRegisterAddress());
		custModel.setLegalPerson(cust.getLegalPerson());
		custModel.setLegalPersonIdcard(cust.getLegalPersonIdcard());
		custModel.setRegisterNo(cust.getRegisterNo());
		custModel.setTaxNo(cust.getTaxNo());
		custModel.setContactPerson(cust.getContactPerson());
		custModel.setContactWay(cust.getContactWay());
		custModel.setFoundTime(new Date(cust.getFoundTime()));
		custModel.setRegisterStartTime(new Date(cust.getRegisterStartTime()));
		custModel.setRegisterEndTime(new Date(cust.getRegisterEndTime()));
		custModel.setCurrencyId(cust.getCurrencyId());
		custModel.setTaxpayerType(cust.getTaxpayerType()+"");

		// 其他信息
		custModel.setCorpId(cust.getCorpId());
		List<CustAccountModel> accountModels = new ArrayList<CustAccountModel>();
		List<CustAccountEntity> accounts = cust.getCustAccounts();
		for (CustAccountEntity custAccountEntity : accounts) {
			accountModels.add(custAccountEntityToModel(custAccountEntity));
		}
		custModel.setCustAccountList(accountModels);

		long count = getCustDAO().childrenCount(cust.getId());
		custModel.setHasChildren(count > 0);

		return custModel;
	}

	private CustAccountModel custAccountEntityToModel(CustAccountEntity custAccount) {
		CustAccountModel custAccountModel = new CustAccountModel();
		custAccountModel = super.ocEntityToModel(custAccountModel, custAccount);
		custAccountModel.setId(custAccount.getId());
		custAccountModel.setAccountIsDefault(custAccount.isAccountIsDefault());
		custAccountModel.setAccountNo(custAccount.getAccountNo());
		custAccountModel.setAccountType(custAccount.getAccountType());
		custAccountModel.setBankId(custAccount.getBankId());
		custAccountModel.setRemark(custAccount.getRemark());
		return custAccountModel;
	}

	public List<CustModel> getPagingCust(CustQC qc, int start, int max)	throws ServiceException {
		List<Long> ids = getCustDAO().getPagingCustIdsByCondition(qc,start,max);
		if(ids!=null&&ids.size()>0){
			Long[] sids = new Long[ids.size()];
			ids.toArray(sids);
			return getCustModelByIds(sids);
		}
		return null;
	}

	public Long getCustSizeByCondition(CustQC qc) throws ServiceException {
		return getCustDAO().counterByCondition(qc);
	}

}
